package com.bookshop.user.data;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.bookshop.dataOperator.DatabaseUtils;
import com.bookshop.promotion.data.PersonalPromotionMng;
import com.bookshop.promotion.logic.DiscountCoupons;
import com.bookshop.promotion.logic.EqualCoupons;
public class SendCouponsOut implements CustomerCommonData{
	
	PersonalPromotionMng ppmMng;
	double mCreditMin;
	double mCreditMax;

	public boolean execute(double pCreditMin,double pCreditMax,EqualCoupons pEqualCoupons,DiscountCoupons pDiscountCoupons){
		mCreditMin=pCreditMin;
		mCreditMax=pCreditMax;
		ResultSet rs=DatabaseUtils.getResultSet(CUSTOMER_TABLE,IDENTIFICATION,"customer");
		try {
			while (rs.next()) {
				if(isInRange(rs.getDouble(INDEX_CREDITS+1))){
					String account=rs.getString(INDEX_CUSTOMER_ACCOUNT+1);
					ppmMng=new PersonalPromotionMng(account);
					boolean b1=ppmMng.addOneDiscCoupon(pDiscountCoupons);
					boolean b2=ppmMng.addOneEqualCoupon(pEqualCoupons);
					if(!(b1&&b2))
						return false;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	private boolean isInRange(double pCreditActual){
		if(pCreditActual>=mCreditMin){
		    if(mCreditMin>=mCreditMax||pCreditActual<=mCreditMax)
		    	return true;
		}
		return false;
	}
}
